package com.dykj.fireFighting.web.devicemanger;

import java.util.*;

import com.dykj.fireFighting.api.socialUnit.devicemanger.dto.EscapeRouteDto;
import com.dykj.fireFighting.api.socialUnit.devicemanger.entity.EscapeRoute;
import com.dykj.fireFighting.api.socialUnit.devicemanger.service.IEscapeRouteService;
import com.dykj.fireFighting.common.core.annotation.AutoLog;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.dto.CommonDto;
import com.dykj.fireFighting.common.core.exception.BaseException;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;


import javax.validation.Valid;



/**
 * 逃生通道表接口
 *
 * @Author JacksonTu
 * @Date  2021-09-09 15:42:37
 */
@Slf4j
@Api(value = "逃生通道表接口", tags = {"逃生通道表接口"})
@RestController
@RequestMapping("devicemanger/escapeRoute")
public class EscapeRouteController {

    @DubboReference
    private IEscapeRouteService escapeRouteService;

    /**
     * 获取用户名称
     * @return String
     */
    public String getName() {
        try {
            return ShiroUtil.getUser().getName();
        } catch (NullPointerException e) {
            e.printStackTrace();
            throw new BaseException("请登录系统");
        }
    }

    /**
     * 获取单位id
     * @return String
     */
    public String getEnterpriseId() {
        try {
            return Long.toString(ShiroUtil.getUser().getEnterpriseId());
        } catch (NullPointerException e) {
            e.printStackTrace();
            throw new BaseException("请登录系统");
        }
    }



    /**
     * 逃生通道表列表
     *
     * @return
     */
    @AutoLog(value = "逃生通道表-分页列表查询")
    @ApiOperation(value = "逃生通道表分页列表", notes = "逃生通道表分页列表")
    @GetMapping("/list")
    public CommonResult<PageVo<EscapeRoute>> treeGrid(EscapeRouteDto commonDto) {
        commonDto.setUnitId(getEnterpriseId());
        PageVo<EscapeRoute> page = escapeRouteService.pageList(commonDto);
        return CommonResult.success(page);
    }

    /**
     * 获取逃生通道表信息
     */
    @AutoLog(value = "逃生通道表-获取逃生通道表信息")
    @ApiOperation(value = "获取逃生通道表信息", notes = "获取逃生通道表信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", dataType = "String", required = true)
    @GetMapping("/info/{id}")
    public CommonResult<EscapeRoute> info(@PathVariable("id") String id){
        EscapeRoute escapeRoute = escapeRouteService.getById(id);
        return CommonResult.success(escapeRoute);
    }

    /**
     * 保存逃生通道表信息
     */
    @AutoLog(value = "逃生通道表-保存逃生通道表信息")
    @ApiOperation(value = "保存逃生通道表信息", notes = "保存逃生通道表信息")
    @PostMapping("/save")
    public CommonResult save(@Valid @RequestBody EscapeRoute escapeRoute) {
        try {
            escapeRoute.setCreateTime(new Date());
            escapeRoute.setUnitId(getEnterpriseId());
            escapeRoute.setCreateBy(getName());
            escapeRoute.setUnitId(getEnterpriseId());
                escapeRouteService.save(escapeRoute);
            return CommonResult.success("添加成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 修改逃生通道表信息
     */
    @AutoLog(value = "逃生通道表-修改逃生通道表信息")
    @ApiOperation(value = "修改逃生通道表信息", notes = "修改逃生通道表信息")
    @PostMapping("/update")
    public CommonResult update(@Valid @RequestBody EscapeRoute escapeRoute) {
        try {
            escapeRoute.setUpdateTime(new Date());
            escapeRoute.setUpdateBy(getName());
                escapeRouteService.updateById(escapeRoute);
            return CommonResult.success("修改成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 删除逃生通道表信息
     */
    @AutoLog(value = "逃生通道表-删除逃生通道表信息")
    @ApiOperation(value = "删除逃生通道表信息", notes = "删除逃生通道表信息")
    @ApiImplicitParam(paramType = "query", name = "ids", value = "ID数组", dataType = "String", required = true, allowMultiple = true)
    @PostMapping("/delete")
    public CommonResult delete(@RequestBody String[]ids) {
        try {
                escapeRouteService.removeByIds(Arrays.asList(ids));
            return CommonResult.success("删除成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }
}
